package com.hetang.maijiayun.adaptertools.adapter;

import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import com.yhd.YhdClient;
import com.yhd.object.order.Order;
import com.yhd.request.order.OrdersGetRequest;
import com.yhd.response.order.OrdersGetResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.impl.cookie.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wanda on 11/4/15.
 */
public class YihaodianAdapter extends AdapterAbstract {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(YihaodianAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该一号店平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        YhdClient client = new YhdClient(authorizesMap.get("url"), authorizesMap.get("appKey"), authorizesMap.get("secret"));
        OrdersGetRequest req = new OrdersGetRequest();
        req.setOrderStatusList("ORDER_WAIT_PAY,ORDER_PAYED,ORDER_WAIT_SEND,ORDER_ON_SENDING,ORDER_RECEIVED,ORDER_FINISH,ORDER_CANCEL");
        req.setDateType(5);
        req.setStartTime(DateUtils.formatDate(request.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setEndTime(DateUtils.formatDate(request.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setCurPage(1);
        req.setPageRows(50);
        logger.info("yhd get order interface, request time is : {}, request param is : {}, authorizesMap : {}", System.currentTimeMillis(), JsonTools.createJsonString(req), JsonTools.createJsonString(authorizesMap));
        OrdersGetResponse rsp = client.excute(req);
        logger.info("yhd get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (null == req || 0 != rsp.getErrorCount().intValue()) {
            return null;
        }
        Long page = 0L;
        Long mod = rsp.getTotalCount()%50L;
        page = rsp.getTotalCount()/50L;
        if (0 != mod) {
            page += 1;
        }
        List<String> result = new ArrayList<String>();
        while (page > 0) {
            req.setCurPage(page.intValue());
            rsp = client.excute(req);
            if (!"".equals(rsp.getErrorCount()) && !"0".equals(rsp.getErrorCount())) {
                logger.error("yhd get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getOrderList().getOrder())) {
                continue;
            }
            for (Order order : rsp.getOrderList().getOrder()) {
                result.add(order.getOrderId() + "");
            }
            page -- ;
        }
        return result;
    }
}
